﻿/*=================================================================
* 版权信息：Copyright (C) 2005-2011 捷点科技 All Rights Reserved.
* 文件名称：CNVP_GetAddress.cs
* 创建标识：曾乃强 15158756652 2011-03-17 11:03:01
* 修改标识：
* 功能描述：
===================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CNVP.Framework.Helper;
using CNVP.Config;

namespace CNVP.JShop.DAL
{
    /// <summary>
    /// 对*CNVP_GetAddress*操作数据库(针对SQL Server)的实现
    /// </summary>
    public class CNVP_GetAddress
    {
       private string Prefix = DBConfig.Prefix;
        #region DAL
        public int Insert(Model.CNVP_GetAddress obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into  " + Prefix + "GetAddress ([TrueName],[MobilePhone],[Phone],[Province],[City],[Area],[GetAddress],[UsersID], [ZipCode], [IsTolerant])");
            sql.Append("values(@TrueName,@MobilePhone,@Phone,@Province,@City,@Area,@GetAddress,@UsersID, @ZipCode, @IsTolerant)");
            
            SqlParameter[] pra = {
                            new SqlParameter("@TrueName",obj.TrueName),
							new SqlParameter("@MobilePhone",obj.MobilePhone),
							new SqlParameter("@Phone",obj.Phone),
							new SqlParameter("@Province",obj.Province),
							new SqlParameter("@City",obj.City),
							new SqlParameter("@Area",obj.Area),
							new SqlParameter("@GetAddress",obj.GetAddress),
							new SqlParameter("@UsersID",obj.UsersID),
                            new SqlParameter("@ZipCode",obj.ZipCode),
                            new SqlParameter("@IsTolerant",obj.IsTolerant)
                                 };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public int Update(Model.CNVP_GetAddress obj)
        {
             StringBuilder sql = new StringBuilder();
             sql.Append("update " + Prefix + "GetAddress set [TrueName] = @TrueName,[MobilePhone] = @MobilePhone,[Phone] = @Phone,[Province] = @Province,[City] = @City,[Area] = @Area,[GetAddress] = @GetAddress,[ZipCode]=@ZipCode, [IsTolerant]=@IsTolerant where [ID] = @ID");

            SqlParameter[] pra = { 
                            new SqlParameter("@ID",obj.ID),
							new SqlParameter("@TrueName",obj.TrueName),
							new SqlParameter("@MobilePhone",obj.MobilePhone),
							new SqlParameter("@Phone",obj.Phone),
							new SqlParameter("@Province",obj.Province),
							new SqlParameter("@City",obj.City),
							new SqlParameter("@Area",obj.Area),
							new SqlParameter("@GetAddress",obj.GetAddress),
                            new SqlParameter("@ZipCode",obj.ZipCode),
                            new SqlParameter("@IsTolerant",obj.IsTolerant)
                                };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public int Delete(Model.CNVP_GetAddress obj)
        {
           StringBuilder sql = new StringBuilder();
           sql.Append("delete from " + Prefix + "GetAddress where [ID] = @ID");
           SqlParameter[] pra = {
                                new SqlParameter("@ID",obj.ID)
                                };
           return DbHelper.ExecuteNonQuery(sql.ToString(),pra);
        }

        public Model.CNVP_GetAddress LoadById(int ID)
        {

            StringBuilder sql = new StringBuilder();
            sql.Append("select [TrueName],[MobilePhone],[Phone],[Province],[City],[Area],[GetAddress],[UsersID],[ZipCode], [IsTolerant] from " + Prefix + "GetAddress");
            sql.Append(" where [ID] = @ID");
            SqlParameter[] pra = {
                                 new SqlParameter("@ID",ID)
                                    };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(), pra);
            Model.CNVP_GetAddress cnvp_getaddress = new Model.CNVP_GetAddress();
            if (DT != null && DT.Rows.Count > 0)
            {
                DataRow dr = DT.Rows[0];
                cnvp_getaddress.TrueName = dr["TrueName"] is DBNull ? String.Empty : (string)dr["TrueName"];
                cnvp_getaddress.MobilePhone = dr["MobilePhone"] is DBNull ? String.Empty : (string)dr["MobilePhone"];
                cnvp_getaddress.Phone = dr["Phone"] is DBNull ? String.Empty : (string)dr["Phone"];
                cnvp_getaddress.Province = dr["Province"] is DBNull ? String.Empty : (string)dr["Province"];
                cnvp_getaddress.City = dr["City"] is DBNull ? String.Empty : (string)dr["City"];
                cnvp_getaddress.Area = dr["Area"] is DBNull ? String.Empty : (string)dr["Area"];
                cnvp_getaddress.GetAddress = dr["GetAddress"] is DBNull ? String.Empty : (string)dr["GetAddress"];
                cnvp_getaddress.UsersID = dr["UsersID"] is DBNull ? 0 : (int)dr["UsersID"];
                cnvp_getaddress.ZipCode = dr["ZipCode"] is DBNull ? String.Empty : (string)dr["ZipCode"];
                cnvp_getaddress.IsTolerant = dr["IsTolerant"] is DBNull ? (byte)0 : (byte)dr["IsTolerant"];
            }
            return cnvp_getaddress;
        }

        /// <summary>
        /// 分页
        /// </summary>
        public DataTable GetPage(string PageName, int PageIndex, int PageSize, out int RecordCount, out int PageCount, params SQLConditionInfo[] SqlCondition)
        {
            try
            {
                string sql_Temp = "";
                if (SqlCondition[0].value.ToString() != "")
                {
                    sql_Temp += "where [UsersID]=" + SqlCondition[0].value.ToString();
                }
                string AllFields = "[ID],[TrueName],[MobilePhone],[Phone],[Province],[City],[Area],[GetAddress],[UsersID],[ZipCode], [IsTolerant] ";
                string Condition = "" + Prefix + "GetAddress " + sql_Temp + "";
                string IndexField = "ID";
                string OrderFields = "order by ID Desc";
                return DbHelper.ExecutePage(AllFields, Condition, IndexField, OrderFields, PageIndex, PageSize, out RecordCount, out PageCount, null);
            }
            catch (Exception e)
            {
                throw new Exception("用于分页的SQL语句无效:" + e.Message);
            }

        }
        #endregion

        #region 设置默认时
        /// <summary>
        /// 把为‘1’全部更新设置成‘0’
        /// </summary>
        public int UpdateWhereOne(int UsersID)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update " + Prefix + "GetAddress set [IsTolerant] = 0 where UsersID=@UsersID and  [IsTolerant] = 1");
            SqlParameter[] pra = {
                                 new SqlParameter("@UsersID",UsersID)
						
                                    };
            return DbHelper.ExecuteNonQuery(sql.ToString(),pra);
        }
        #endregion

        public List<Model.CNVP_GetAddress> LoadAllByUsersID(int UsersID)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select [ID],[TrueName],[MobilePhone],[Phone],[Province],[City],[Area],[GetAddress], [ZipCode], [IsTolerant] from " + Prefix + "GetAddress");
            sql.Append(" where [UsersID] = @UsersID");
            SqlParameter[] pra = {
                                 new SqlParameter("@UsersID",UsersID)
                                    };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(),pra);
            List<Model.CNVP_GetAddress> list = new List<Model.CNVP_GetAddress>();
            if (DT != null && DT.Rows.Count > 0)
            {
                foreach (DataRow dr in DT.Rows)
                {
                    Model.CNVP_GetAddress cnvp_getaddress = new Model.CNVP_GetAddress();
                    cnvp_getaddress.ID = dr["ID"] is DBNull ? 0 : (int)dr["ID"];
                    cnvp_getaddress.TrueName = dr["TrueName"] is DBNull ? String.Empty : (string)dr["TrueName"];
                    cnvp_getaddress.MobilePhone = dr["MobilePhone"] is DBNull ? String.Empty : (string)dr["MobilePhone"];
                    cnvp_getaddress.Phone = dr["Phone"] is DBNull ? String.Empty : (string)dr["Phone"];
                    cnvp_getaddress.Province = dr["Province"] is DBNull ? String.Empty : (string)dr["Province"];
                    cnvp_getaddress.City = dr["City"] is DBNull ? String.Empty : (string)dr["City"];
                    cnvp_getaddress.Area = dr["Area"] is DBNull ? String.Empty : (string)dr["Area"];
                    cnvp_getaddress.GetAddress = dr["GetAddress"] is DBNull ? String.Empty : (string)dr["GetAddress"];
                    cnvp_getaddress.ZipCode = dr["ZipCode"] is DBNull ? String.Empty : (string)dr["ZipCode"];
                    cnvp_getaddress.IsTolerant = dr["IsTolerant"] is DBNull ? (byte)0 : (byte)dr["IsTolerant"];
                    list.Add(cnvp_getaddress);
                }
            }
            return list;

        }


        public int UpdateWithout(Model.CNVP_GetAddress obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update " + Prefix + "GetAddress set [TrueName] = @TrueName,[MobilePhone] = @MobilePhone,[Phone] = @Phone,[Province] = @Province,[City] = @City,[Area] = @Area,[GetAddress] = @GetAddress,[ZipCode]=@ZipCode where [ID] = @ID");

            SqlParameter[] pra = { 
                            new SqlParameter("@ID",obj.ID),
							new SqlParameter("@TrueName",obj.TrueName),
							new SqlParameter("@MobilePhone",obj.MobilePhone),
							new SqlParameter("@Phone",obj.Phone),
							new SqlParameter("@Province",obj.Province),
							new SqlParameter("@City",obj.City),
							new SqlParameter("@Area",obj.Area),
							new SqlParameter("@GetAddress",obj.GetAddress),
                            new SqlParameter("@ZipCode",obj.ZipCode)
                                };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }
    }
}

